package com.demo03.test01;

import java.util.concurrent.*;

public class ThreadPoolTest {
    public static void main(String[] args) {

        ExecutorService singleThreadPool = new ThreadPoolExecutor(5, 20,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024), new ThreadPoolExecutor.AbortPolicy());

        for (int j = 0; j < 20; j++) {
            final int task = j;
            singleThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " : " + i + " : " + task);
                    }
                }
            });
        }
        singleThreadPool.shutdown();


        ExecutorService threadPool = new ThreadPoolExecutor(1,
                1,
                0L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1024),
                new ThreadPoolExecutor.AbortPolicy());
        Future<Object> future = threadPool.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                return "hello world";
            }
        });

        try {
            Thread.sleep(10000);
            System.out.println("Future : " + future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        threadPool.shutdown();

        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaa");

    }
}



